﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using Compiled_Tests.AutomationAbstractions;
using Tedds_Tool.Instructions;
using Tedds_Tool.Instructions.Actions;
using CommonInterfaces.Models;
using StandardScriptItems.SimpleControls;

namespace Tedds_Tool.Automatables
{
    public class ExpandCollapseable : ILEETActionModual
    {
        public AutomationElementEventListener CreateListener(IObservableWidget elem, IRegistryAccessor reg)
        {
            return new EventListener(elem, reg);
        }

        public bool CanCompile(string instruction)
        {
            try
            {
                return Instruction.apply(instruction) is ExpndColps;

            }
            catch (InvalidInstructionException)
            {
                return false;
            }
        }

        public IInstruction CreateInstruction(string instruction)
        {
            return Instruction.apply(instruction);
        }

        

        private class EventListener : AutomationElementEventListenerBase
        {
            
            AutomationPropertyChangedEventHandler handler;
            private IRegistryAccessor registry;

            public EventListener(IObservableWidget target, IRegistryAccessor reg)
            {
                this.RootTarget = target;
                this.registry = reg;
                Listening = false;
                try
                {
                    handler = new AutomationPropertyChangedEventHandler(CatchEvent);
                    registry.RegisterEventHandler(ExpandCollapsePattern.ExpandCollapseStateProperty, RootTarget.ae, handler);
                    Listening = true;
                }
                catch (ArgumentException) { }
            }

            public override void Listen(IObservableWidget widget, String property, String newValue)
            {
                //In case element disappeared before it could be processed
                if (widget == null) return;

                string element = widget.GetUIAHierarchy(RootTarget).ToString();
                string state;
                if (newValue == "1")
                    state = ExpandCollapseState.Expanded.ToString();
                else
                    state = ExpandCollapseState.Collapsed.ToString();

                Delagate(new Event_Pair("ACTION\t| EXPNDCOLPS | " + element + " | " + state));

            }

            public override void Dispose()
            {
                registry.RemoveEventHandler(ExpandCollapsePattern.ExpandCollapseStateProperty, RootTarget.ae, handler);
                base.Dispose();
            }

        }

        public void Initialize()
        {
        }

        public string TypeName
        {
            get { return "EXPNDCOLPS"; }
        }

        public System.Windows.UIElement CreateScriptItemDetails()
        {
            throw new NotImplementedException();
        }


        public System.Windows.UIElement CreateScriptItemDetails(LEET.CommonInterfaces.Nil onUpdate, IEnumerable<string> processes, IElementState state)
        {
            ElementStateComboBox elem = new ElementStateComboBox(state);
            ScriptItemArgumentSelection arg1 = new ScriptItemArgumentSelection(elem, new String[] { "Expanded", "Collapsed" });
            return arg1;
        }
    }
}
